import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class TestSet {
	public TestSet(Integer howManyQuestions,
			Map<Integer, Question> questionMap, Integer[] mandatoryQuestions)
			throws Exception {
		if (mandatoryQuestions == null
				|| howManyQuestions < mandatoryQuestions.length)
			throw new Exception("howManyQuestions < mandatoryQuestions.length");

		this.qid2OrgQid = new Integer[howManyQuestions];

		Random r = new Random();

		Set<Integer> qAlreadyUsedInThisSet = new HashSet<Integer>();
		for (Integer mandatoryQuestion : mandatoryQuestions) {
			int qPos = 0;
			do {
				qPos = r.nextInt(howManyQuestions);
			} while (this.qid2OrgQid[qPos] != null);
			this.qid2OrgQid[qPos] = mandatoryQuestion;

			qAlreadyUsedInThisSet.add(mandatoryQuestion);
			if (!TestSet.qAlreadyUsed.contains(mandatoryQuestion))
				TestSet.qAlreadyUsed.add(mandatoryQuestion);
		}

		for (int i = 0; i < howManyQuestions; i++) {
			if (this.qid2OrgQid[i] != null)
				continue;

			int orgQid = 0;
			do {
				orgQid = r.nextInt(questionMap.size() - 1) + 1;
			} while (TestSet.qAlreadyUsed.contains(orgQid));
			this.qid2OrgQid[i] = orgQid;
			qAlreadyUsedInThisSet.add(orgQid);
			TestSet.qAlreadyUsed.add(orgQid);
		}
	}

	public static Set<Integer> getqAlreadyUsed() {
		return qAlreadyUsed;
	}

	public Map<Integer, Integer> getQid2OrgQid() throws Exception {
		if (this.qid2OrgQid == null)
			throw new Exception("this.qid2OrgQid == null");
		Map<Integer, Integer> qid2OrgQidMap = new HashMap<Integer, Integer>(
				this.qid2OrgQid.length);
		for (int i = 0; i < this.qid2OrgQid.length; i++) {
			qid2OrgQidMap.put((i + 1), this.qid2OrgQid[i]);
		}
		return qid2OrgQidMap;
	}

	private static Set<Integer> qAlreadyUsed = new HashSet<Integer>();
	private Integer[] qid2OrgQid = null;
}
